<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('productCenter.appConfig.SMS.state')">
          <el-select v-model="searchForm.status" clearable style="width: 100%">
            <el-option
              v-for="item in $t('productCenter.appConfig.SMS.status')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('productCenter.appConfig.SMS.theme')">
          <el-input v-model="searchForm.theme" :placeholder="$t('productCenter.appConfig.SMS.prompt.theme')" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="id"
          :label="tableText['id']"
          min-width="50"
        />
        <el-table-column
          fixed="left"
          prop="theme"
          :label="tableText['theme']"
          min-width="120"
        />
        <el-table-column
          prop="msgText"
          :label="tableText['content']"
          min-width="250"
        />
        <el-table-column
          prop="modifier"
          :label="tableText['updater']"
          min-width="100"
        />
        <el-table-column
          prop="modifyTimeStr"
          :label="tableText['updateTime']"
          min-width="170"
        />
        <el-table-column
          fixed="right"
          prop="status"
          :label="tableText['state']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row['status'] ? $t('productCenter.appConfig.SMS.status')[1]['desc'] : $t('productCenter.appConfig.SMS.status')[2]['desc'] }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="editItem(scope.row)">{{ $t('common.operate.edit') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <sms-template-dialog
      :show-dialog="editDialog"
      :choose-item="chooseItem"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
  </el-row>
</template>

<script>
import { getSMS } from '@/api/product'
import SmsTemplateDialog from '@/components/product/sms-template-dialog'
export default {
  name: 'SMSTemplate',
  components: {
    SmsTemplateDialog
  },
  data() {
    return {
      loading: false,
      editDialog: false,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      searchForm: this.params(),
      chooseItem: {
        id: null,
        theme: null,
        msgText: null,
        status: null,
        templateCode: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('productCenter.appConfig.SMS')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        theme: null,
        status: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    getData() {
      this.loading = true
      getSMS(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    editItem(item) {
      this.choose(item)
      this.editDialog = true
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.theme = param.theme
      this.chooseItem.msgText = param.msgText
      this.chooseItem.status = param.status
      this.chooseItem.templateCode = param.templateCode
    },
    editSuccess() {
      this.getData()
    },
    changeDialog(param) {
      this.editDialog = param
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
</style>
